# encoding:utf8
import hashlib
import logging
import os


class CmpRes(object):
    ''' 打包资源的脚本 '''
    @staticmethod
    def path_md5_diffres(frompath, basepath, ignorefunc):
        '''  
        查看资源文件路径的md5值
        '''
        ret = dict()
        if frompath.split(os.sep)[-1] is "res":
            logging.error("Packres dir must start with <res>!")
            return

        def _getmd5(fileobj):
            m5 = hashlib.md5()
            data = open(fileobj, "rb")
            while True:
                tdata = data.read(1024)
                if not tdata:
                    break
                m5.update(tdata)
            data.close()
            return m5.hexdigest()

        def _iter_res(tarpath):
            for filename in os.listdir(tarpath):
                fileobj = os.path.join(tarpath, filename)
                if os.path.isfile(fileobj):
                    if ignorefunc(fileobj) == False:
                        ret[fileobj.replace(basepath, "")] = _getmd5(fileobj)
                    else:
                        logging.info(
                            "The file is ignore by the rule!--->>%s", fileobj)
                else:
                    _iter_res(fileobj)

        _iter_res(frompath)
        return ret
